package com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest

import com.lancoo.znbkxx.teacher.Ext
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import java.net.URLDecoder


/**
 * Created by Dadong on 2020/5/15
 * Describe:
 */
class MoreBaseUrlInterceptor : Interceptor {

    private lateinit var newHttpUrl: HttpUrl
    private lateinit var newRequest: Request

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        //获取原始的originalRequest
        val originalRequest: Request = chain.request()
        //获取老的url
        var oldUrl: HttpUrl = originalRequest.url
        //获取originalRequest的创建者builder
        val builder: Request.Builder = originalRequest.newBuilder()
        //获取头信息的集合如：manage,mdffx
        val urlNameList: List<String> =
            originalRequest.headers("urlName")
        return if (urlNameList.isNotEmpty()) {
            //删除原有配置中的值,就是namesAndValues集合里的值
            builder.removeHeader("urlName")
            //获取头信息中配置的value,如：manage或者mdffx
            val urlName = urlNameList[0]
            var baseURL: HttpUrl? = null
            //根据头信息中配置的value,来匹配新的base_url地址
            if ("sc" == urlName) {
                baseURL = Ext.WORDS_URL.toHttpUrlOrNull()
                newHttpUrl = setUrl(baseURL!!, oldUrl)
            } else if ("base" == urlName) {
                baseURL = Ext.PLATFORM_URL.toHttpUrlOrNull()
                newHttpUrl = setUrl(baseURL!!, oldUrl)
            } else if ("oneOnOne" == urlName) {
                baseURL = Ext.ONEONONE_URL.toHttpUrlOrNull()
                newHttpUrl = setUrl(baseURL!!, oldUrl)
            }else if ("rzzd" == urlName) {
                baseURL = Ext.RZZD_URL.toHttpUrlOrNull()
                if (baseURL!!.pathSegments[0] != "") {
                    if (oldUrl.encodedQuery.isNullOrEmpty()) {
                        newHttpUrl = HttpUrl
                                .Builder()
                                .scheme(baseURL.scheme) //http协议如：http或者https
                                .host(baseURL.host) //主机地址
                                .port(baseURL.port) //端口
                                .addPathSegments(baseURL.encodedPath.substring(1))
                                .addPathSegments(oldUrl.encodedPath.substring(1))
                                .build()
                    } else {
                        newHttpUrl = HttpUrl
                                .Builder()
                                .scheme(baseURL.scheme) //http协议如：http或者https
                                .host(baseURL.host) //主机地址
                                .port(baseURL.port) //端口
                                .addPathSegments(baseURL.encodedPath.substring(1))
                                .addPathSegments(oldUrl.encodedPath.substring(1))
                                .query(URLDecoder.decode(oldUrl.encodedQuery, "UTF-8"))
                                .build()
                    }

                } else {
                    newHttpUrl = setUrl(baseURL!!, oldUrl)
                }
            }

            //获取处理后的新newRequest
            newRequest = builder.url(newHttpUrl).build()
            chain.proceed(newRequest)
        } else {
            chain.proceed(originalRequest)
        }
    }

    private fun setUrl(baseURL: HttpUrl, oldUrl: HttpUrl): HttpUrl {
        return oldUrl
                .newBuilder()
                .scheme(baseURL.scheme) //http协议如：http或者https
                .host(baseURL.host) //主机地址
                .port(baseURL.port) //端口
                .build()
    }
}
